"""
Kafka 生产者代码Demo
"""
import time
import json

from pykafka import KafkaClient


def producer_fun(txt: str):
    client = KafkaClient(hosts='10.0.42.160:9092')
    topic = client.topics['testTopic001'.encode()]
    producer = topic.get_sync_producer()
    for v in range(10):
        producer.produce(txt.encode())
        time.sleep(0.2)


if __name__ == '__main__':
    d = {"x":100, "y":100,"time":time.time()}
    txt = json.dumps(d)
    producer_fun(txt)